
use "$project\merger_lbnr.dta", clear

merge m:1 cvrnr year using "$project\zephyr\failtarget.dta", keep(1 3) nogen


bysort lbnr: egen maxnworker=max(nworker)
drop if maxnworker<3
drop if region==.

merge m:1 cvrnr year using "$home\firmdata_v1.dta", keepus (valueadded fulltimeeq/*
*/  sales vapp start_year) nogen keep(1 3)
gen firmage=year-start_year

bysort cvrnr year: gen nestab=_N

*firm FE
merge m:1 lbnr year using "$project\merger_lbnr", keepus(lbnr_merger target) keep(1 3) nogen 
ren lbnr_merger firmid
merge m:1 firmid using "$project\AKM\firmeffs_annualwage_ma_lbnr_separate.dta", keep(1 3) keepus(fe_1) nogen


*calculate characteristics

gen emp_decile=.
gen wage_decile=.
forv yr=1995(1)2010 {
xtile temp_emp_decile=nworker if year==`yr', n(4)
xtile temp_wage_decile=log_income if year==`yr', n(4)
quietly replace emp_decile=temp_emp_decile if year==`yr'
quietly replace wage_decile=temp_wage_decile if year==`yr'
drop temp_emp_decile temp_wage_decile
}
xtile fe_decile=fe_1, n(10)

*estimate propensity score

tsset lbnr year
gen log_nworker=log(nworker)
replace target=(failtarget==1)
cap drop target_lead
gen target_lead=F1.target
gen emp_growth=log(nworker/L.nworker)
gen wage_growth=log_income-L.log_income
gen emp_growth_lag1=log(L.nworker/L2.nworker)
gen wage_growth_lag1=L.log_income-L2.log_income
gen wage_growth2=ln_hrwage-L.ln_hrwage
cap drop nmerger
bysort lbnr: egen nmerger=sum(merger)

xtile decile_emp_growth=emp_growth, n(4)
*xtile decile_emp_growth_lag=emp_growth_lag1, n(2)
xtile decile_wage_growth=wage_growth, n(4)
*xtile decile_wage_growth2=wage_growth2, n(4)

*egen ind36_num=group(ind36)
reg target_lead log_nworker ln_hrwage sh_educ_voc sh_educ_high emp_growth* wage_growth* /*
*/ sh_female avg_age avg_exper vapp fe_1 i.year i.ind127
predict pscore, xb

reg target_lead log_nworker ln_hrwage sh_educ_voc sh_educ_high emp_growth* wage_growth* /*
*/ sh_female avg_age avg_exper fe_1 i.year i.ind127
predict pscore2, xb

*drop establishments that are acquired twice
cap drop ntarget
bysort lbnr: egen ntarget=sum(target)
drop if ntarget>=2

*matching
egen matchinggroup=group(year region ind36 wage_decile emp_decile decile_wage_growth)
bysort matchinggroup: gen nestab_group=_N

bysort target_lead: gen id=_n
replace id=. if target_lead==0|target_lead==.

*drop if ambiguous or exiting
drop if target_lead==.
drop if matchinggroup==.

sort matchinggroup pscore
by matchinggroup: replace id=id[_n-1] if target_lead[_n-1]==1&nmerger==0&id==.&pscore[_n-1]!=.&pscore!=.
by matchinggroup: replace id=id[_n+1] if target_lead[_n+1]==1&nmerger==0&id==.&pscore[_n+1]!=.&pscore!=.
bysort id: egen temp_pscore=max(target_lead*pscore)
gen temp_diff=(pscore-temp_pscore)^2
sort id temp_diff
by id: gen temp_order=_n
replace id=. if temp_order>2
bysort id: gen temp_count=_N
gen matched=1 if temp_count==2
bysort lbnr: egen temp_nmatched=sum(matched)
drop if temp_nmatched>0&matched==.
sort lbnr temp_diff
by lbnr: gen temp_order2=_n
drop if temp_nmatched>1&temp_order2>1
bysort id: gen temp_count2=_N
replace matched=. if temp_count2==1
drop temp*

sort matched matchinggroup pscore
by matched matchinggroup: replace id=id[_n-1] if target_lead[_n-1]==1&nmerger==0&id==.&pscore[_n-1]!=.&pscore!=.&matched==.
by matched matchinggroup: replace id=id[_n+1] if target_lead[_n+1]==1&nmerger==0&id==.&pscore[_n+1]!=.&pscore!=.&matched==.
bysort id: egen temp_pscore=max(target_lead*pscore)
gen temp_diff=(pscore-temp_pscore)^2
sort id temp_diff
by id: gen temp_order=_n
replace id=. if temp_order>2
bysort id: gen temp_count=_N
replace matched=1 if temp_count==2
bysort lbnr: egen temp_nmatched=sum(matched)
drop if temp_nmatched>0&matched==.
sort lbnr temp_diff
by lbnr: gen temp_order2=_n
drop if temp_nmatched>1&temp_order2>1
bysort id: gen temp_count2=_N
replace matched=. if temp_count2==1
drop temp*

sort matched matchinggroup pscore2
by matched matchinggroup: replace id=id[_n-1] if target_lead[_n-1]==1&nmerger==0&id==.&pscore2[_n-1]!=.&pscore2!=.&matched==.
by matched matchinggroup: replace id=id[_n+1] if target_lead[_n+1]==1&nmerger==0&id==.&pscore2[_n+1]!=.&pscore2!=.&matched==.
bysort id: egen temp_pscore2=max(target_lead*pscore2)
gen temp_diff=(pscore2-temp_pscore2)^2
sort id temp_diff
by id: gen temp_order=_n
replace id=. if temp_order>2
bysort id: gen temp_count=_N
replace matched=1 if temp_count==2
bysort lbnr: egen temp_nmatched=sum(matched)
drop if temp_nmatched>0&matched==.
sort lbnr temp_diff
by lbnr: gen temp_order2=_n
drop if temp_nmatched>1&temp_order2>1
bysort id: gen temp_count2=_N
replace matched=. if temp_count2==1
drop temp*

keep if matched==1

cap drop target_year
bysort id: egen target_year=max(target_lead*year)
replace target_year=target_year+1

save "$project\matching_failtarget", replace



use "$home\persondata_v3.dta", clear

*tsset pnr year
*tsfill
*gen byte fill=(lbnr==.)
*append using "I:\Workdata\702525\Alex\ind\ind_notmatched"

/* Rename variables */
rename akasst year_uifund
rename arblhu ui
ren koen sex
ren IE_TYPE immigrant
ren arledgr frac_unemployed
ren brutto gross_income
ren perindkp total_income
ren civst married
ren tlonkval quality_hour
ren aldernov age
ren koejd housing
gen ln_hrwage=log(timelon)
gen log_income=log(lonind)
gen max_educ=int(hffsp/1000000)
gen educ=1 if max_educ<=25
replace educ=2 if max_educ==35
replace educ=3 if max_educ>=40&max_educ<=50
replace educ=4 if max_educ>=60&max_educ<.
gen byte female=(sex==2)
destring ansaar, replace
gen tenure=year-ansaar
replace tenure=. if tenure>exper
bysort pnr lbnr: egen temp_firstyear=min(year)
by pnr: egen temp_firstyearever=min(year)
replace tenure=year-temp_firstyear if tenure==.&temp_firstyear!=temp_firstyearever
drop temp*
drop ansaar

*worker relative rank
sort lbnr year log_income
by lbnr year: gen rank=_n
by lbnr year: gen nworker=_N
replace rank=rank/nworker

/* Get industry info */
gen pb=persbrc
merge m:1 pb year using "$home\industrycodes\crosswalk_pb07", nogen keep(1 3)
replace pb07=persbrc if pb07==.
merge m:1 pb07 using "$home\industrycodes\ind36", nogen keep(1 3)
merge m:1 pb07 using "$home\industrycodes\ind127", nogen keep(1 3)
replace ind36="X" if ind36==""
replace ind127=99999 if ind127==.
drop branche persbrc pb

merge m:1 lbnr year using "$project\matching_failtarget", keepus(matched target_year target_lead id) keep(1 3) nogen

replace matched=0 if ind36=="O"|ind36=="P"|ind36=="QA"|ind36=="R"|ind36=="QB"
replace id=. if ind36=="O"|ind36=="P"|ind36=="QA"|ind36=="R"|ind36=="QB"

*drop if no control group
*bysort id: egen nfirm_id=nvals(lbnr)
*replace matched=0 if nfirm_id!=2

replace matched=0 if matched==.
bysort pnr: egen nmatched=sum(matched)
bysort lbnr: egen matched_firm=max(matched)
keep if nmatched==1|matched_firm==1

ren target_year temp
bysort lbnr: egen target_year=max(temp*matched)
bysort lbnr: egen treated_firm=max(target_lead*matched)
bysort pnr: egen temp2=max(temp*matched)
replace target_year=temp2 if target_year==.|target_year==0
gen tau=year-target_year
bysort pnr: egen treated=max(target_lead*matched)
replace treated=1 if treated_firm==1&nmatched==0
replace treated=0 if treated==.

forv i=5(-1)2 {
gen byte taub`i'=(tau==-`i')
gen byte taub`i'_t=(tau==-`i'&treated==1)
}

forv i=0/5 {
gen byte tau`i'=(tau==`i')
gen byte tau`i'_t=(tau==`i'&treated==1)
}

bysort pnr: egen mintau=min(tau)
bysort pnr: egen maxtau=max(tau*(lbnr!=.))
bysort pnr: egen age_event=max(matched*age)
bysort pnr: egen tenure_event=max(matched*tenure)
egen branche=group(ind36)
gen exper_educ1=exper*(educ==1)
gen exper_educ2=exper*(educ==2)
gen exper_female=exper*female
replace age_event=age-tau-1 if nmatched==0

*heterogeneity
merge m:1 isco88 using "$project\occupation\occupation", keep(1 3) keepus(rt1 men1 man1) nogen
xtile routine=rt1, n(3)
replace routine=0 if routine<=2
replace routine=1 if routine==3

gen fcsl=0
replace fcsl=1 if isco88>=5120&isco88<=5123
replace fcsl=1 if isco88>=7140&isco88<=7143
replace fcsl=1 if isco88==9132|isco88==9141|isco88==9142
replace fcsl=1 if isco88==9151|isco88==9152
replace fcsl=1 if isco88==8322|isco88==8323|isco88==8324

*firm FE
merge m:1 lbnr year using "$project\merger_lbnr", keepus(lbnr_merger target) keep(1 3) nogen 
ren lbnr_merger firmid
merge m:1 firmid using "$project\AKM\firmeffs_annualwage_ma_lbnr_separate.dta", keep(1 3) keepus(fe_1) nogen
ren fe_1 fe_lbnr

drop firmid
ren cvrnr firmid
merge m:1 firmid using "$project\AKM\firmeffs_annual_cvrnr.dta", keep(1 3) keepus(fe_1) nogen
ren firmid cvrnr
ren fe_1 fe_cvrnr

*VA
merge m:1 cvrnr year using "$home\firmdata_v1.dta", keepus (valueadded fulltimeeq sales) nogen keep(1 3)
replace ind36="SA" if ind127>=94000&ind127<=96000
replace ind36="LA" if ind127==68001|ind127==68003
replace ind36="LB" if ind127==68002
replace ind36="SB" if ind127==97000
merge m:1 ind36 year using "$home/output_ind36", keep(1 3) nogen
foreach var in valueadded sales {
replace `var'=`var'/output_current*output_chained
}
gen vapp=log(valueadded/fulltimeeq)

merge m:1 cvrnr year using "$project\dhhi2", keep(1 3) nogen
tsset pnr year
gen dhhi_lead=F.dhhi if F.lbnr==lbnr

merge m:1 lbnr year using "$project\diff_fe_vapp", keep(1 3) nogen

merge m:1 lbnr year using "$project\sameindustry", keep(1 3) keepus(sameindustry_lead) nogen

merge m:1 lbnr year using "$project\merger_lbnr", keep(1 3) keepus(ratio_lead) nogen

merge m:1 ind127 using "$project\hhi", nogen
replace high_hhi=high_hhi-1

merge m:1 lbnr year using "$project\merger_lbnr", keepus(muni nest) keep(1 3) nogen
merge m:1 muni year using "$home\muni_population", keep(1 3) nogen

merge m:1 cvrnr year using "$project\multifirm_manager", keep(1 3) nogen

merge m:1 lbnr using "$project\wage_resid_new", keep(1 3) nogen

merge 1:1 pnr year using "$project\ratio", keep(1 3) nogen

merge 1:1 pnr year using "$home\archive\samlesamle1", keepus(lonsystem) keep(1 3) nogen

merge m:1 lbnr year using "$project\agency_sumshare", keep(1 3) nogen

merge m:1 cvrnr year using "$home\fire\fire", keep(1 3) keepus(virkform) nogen

bysort pnr lbnr: egen nmatched_spell=sum(matched)

reghdfe log_income taub3-tau5_t exper_educ1-exper_fem if nmatched==1&tau>=-3&tau<=5&age_event>20&age_event<60, absorb(pnr) vce(cluster lbnr)

reghdfe log_income taub3-tau5_t exper_educ1-exper_fem if nmatched==1&tau>=-3&tau<=5&age_event>20&age_event<60&nmatched_s==1, absorb(pnr#lbnr) vce(cluster lbnr)

